package com.rpframework.website.device.act.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.PayMent;
import com.rpframework.website.device.service.PayMentService;

@Controller
@RequestMapping("/admin/payment")
public class AdminPayMentAct extends AdminAct{
	@Resource PayMentService service;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="roomId", required=false) Integer roomId, 
			HttpSession session,
			Map<Object, Object> model){
		if(pager == null) {
			pager = new Pager();
		}
		if(NumberUtils.isValid(roomId))
		pager.getSearchMap().put("roomId", roomId);//同一个小区的所房间
		pager = service.getPager(pager);
		model.put("roomId", roomId);
		model.put("pager", pager);
		return this.doPackageURI("payment/list");
	}
	
	@RequestMapping("/edit-{id}")
	public String edit(@PathVariable Integer id, 
			@RequestParam(value="roomId", required=false) Integer roomId,
			Map<Object, Object> model,RedirectAttributes attr){
		// get entity by service
		PayMent t = service.select(id);
		Assert.notNull(t,this.getClass().getName() + " Id 异常");
		// model put tablePager and this add redirect page replace modelpager
		model.put("pager", t);
		return this.add(attr, model,roomId);
	}
	@RequestMapping("/add")
	public String add(RedirectAttributes attr,
			Map<Object, Object> model, 
			@RequestParam(value="roomId", required=false) Integer roomId
			){
		model.put("roomId", roomId);
		return this.doPackageURI("payment/edit");
	}
	
	//this funtion is delete 
	@RequestMapping("/delete-{id}")
	public String delete(@PathVariable Integer id ,RedirectAttributes attr){
		//delete entity by service
		boolean flag = service.delete(id);
		if(flag){
			setInfoMsg("删除成功！", attr);
		}else{
			setInfoMsg("删除失败！", attr);
		}
		return redirect("/admin/payment/list");
	}
	
	//this funtion is saveOrUpdate 
	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute PayMent t,
			HttpSession session,
			RedirectAttributes attr){
		boolean flag = service.doSaveOrUpdate(t);
		if(flag){
			if(StringUtils.isNotBlank(t.getId())) {//update
				setInfoMsg("更新操作成功！", attr);
			} else {
				setInfoMsg("新增操作成功！", attr);
			}
		}else{
			setInfoMsg("操作失败！", attr);
		}
		return redirectParam("/admin/payment/list?roomId="+t.getRoomId());
	}
}
